﻿using Growatt.DuiHelpers;
using Growatt.WaveData.VirtualOscilloscope.Frm;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Growatt.WaveData.VirtualOscilloscope
{
    public class WaveDataHelpWin
    {
        #region 显示消息框相关

        private static bool IsRun = false;
        private static object objclock = new object();
        private static List<FrmTipsBox> ls_TipsBox = new List<FrmTipsBox>();

        public static void UpdateUI(Control control, Action action)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (control != null && control.IsHandleCreated)
            {
                if (control.InvokeRequired)//如果调用控件的线程和创建创建控件的线程不是同一个则为True
                {
                    while (!control.IsHandleCreated)
                    {
                        //解决窗体关闭时出现“访问已释放句柄“的异常
                        if (control.Disposing || control.IsDisposed)
                            return;
                    }
                    control.Invoke(action);
                }
                else
                {
                    control.Invoke(action);
                }
            }
        }

        public static Form MajorForm = null;

        ///<summary>
        ///显示短消息
        ///</summary>
        ///<param name="msg"></param>
        public static void ShowMessage(string msg, Control form1 = null)
        {
            try
            {
                if (form1 == null && Program.MainForm != null) { form1 = Program.MainForm; }
                FrmTipsBox box = new FrmTipsBox($"{msg}");
                box.Tag = 6;
                box.BackColor = Color.DodgerBlue;
                box.TopMost = true;
                lock (objclock)
                {
                    ls_TipsBox.Add(box);
                }
                if (!IsRun)
                {
                    Task.Factory.StartNew(() =>
                    {
                        int index = 0;
                        while (true)
                        {
                            try
                            {
                                if (form1 != null && !form1.IsDisposed)
                                {
                                    Control control = form1 as Control;
                                    if (control != null && control.InvokeRequired)
                                    {
                                        control.Invoke(new Action(() => ShowMsg(index, form1)));
                                    }
                                    else
                                    {
                                        ShowMsg(index, form1);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Growatt.DuiHelpers.DuiHelperLog.WriteErrorLog(ex);
                            }

                            Thread.Sleep(200);
                        }
                    });
                    IsRun = true;
                }
            }
            catch (Exception ex)
            {
                Growatt.DuiHelpers.DuiHelperLog.WriteErrorLog(ex);
            }
        }

        private static void ShowMsg(int index, Control Opencontrol)
        {
            try
            {
                lock (objclock)
                {
                    int length = ls_TipsBox.Count;
                    for (int i = 0; i < length; i++)
                    {
                        var msgbox = ls_TipsBox[index];
                        int count = (int)msgbox.Tag;
                        if (count <= 0)
                        {
                            msgbox.Close();
                            msgbox.Dispose();
                            ls_TipsBox.Remove(msgbox);
                            length--;
                            i--;
                        }
                        else
                        {
                            count--;
                            msgbox.Tag = count;
                            Control control = Opencontrol as Control;
                            if (control != null)
                                msgbox.Location = new System.Drawing.Point((control.Location.X + control.Width / 2 - msgbox.Width / 2), control.Location.Y + control.Height - msgbox.Height - 100 + (count % 2 == 0 ? 1 : -1));
                            msgbox.Show();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DuiHelperLog.WriteErrorLog(ex);
            }
        }

        #endregion 显示消息框相关
    }
}
